<template>
    <el-dialog
            :visible.sync="dialogVisible"
            width="400px"
            :before-close="closeDialog"
            append-to-body
            title="添加部门"
    >
    
    
        <el-form  :label-position="labelPosition"  label-width="70px" ref="form" :model="form"  v-loading="loading">
            <el-form-item prop="parentDeptName" label="父组织">
                <el-input size= "small" disabled type="text"  v-model="form.parentDeptName"></el-input>
            </el-form-item>
            <el-form-item prop="deptNo" label="组织编号">
                <el-input size= "small" type="text" placeholder="请输入组织编号" v-model="form.deptNo"></el-input>
            </el-form-item>
            <el-form-item prop="deptCode" label="组织编码">
                <el-input size= "small" type="text" placeholder="请输入组织编码" v-model="form.deptCode"></el-input>
            </el-form-item>
            <el-form-item prop="deptName" label="组织名称">
                <el-input size= "small" type="text" placeholder="请输入组织名称" v-model="form.deptName"></el-input>
            </el-form-item>

            <el-form-item prop="deptLevel" label="组织等级">
                <el-input size= "small" disabled placeholder="系统自动生成" type="text" v-model="form.deptLevel"></el-input>
            </el-form-item>
            <el-form-item prop="deptType" label="组织类型">
                <el-select v-model="form.deptType" placeholder="请选择"  size= "small">
                    <el-option
                    v-for="item in deptTypeOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="order" label="排序">
                <el-input size= "small" placeholder="请输入排序" type="text" v-model="form.order"></el-input>
            </el-form-item>
        </el-form>
        
        <div class="btn-group">
            <el-button type="text" size="small" plain @click="closeDialog">取消</el-button>
            <el-button type="primary"  size="small" :disabled="loading" @click="submit">确定</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import {add} from '@/api/dept'
    export default {
        data(){
            return {
                deptTypeOptions:[
                    {
                        "value":"GROUP",
                        "label":"集团"
                    },{
                        "value":"COMPANY",
                        "label":"公司"
                    },{
                        "value":"MANAGE_DEPT",
                        "label":"管理部门"
                    },{
                        "value":"BUSINESS_DEPT",
                        "label":"业务部门"
                    }
                ],
                labelPosition:'right',
                dialogVisible: false,
                loading: false,
                form: {
                    parentDeptId:"",
                    parentDeptName:"",
                    deptNo:"",
                    deptCode:"",
                    deptName:"",
                    deptType:"",
                    deptLevel:0,
                    order:0
                },
                parent:{},
            }
        },
        created(){

        },
        methods:{
            showDialog(row){
                this.parent = row;
                this.dialogVisible = true
                this.form.parentDeptName = row.deptName;
                this.form.deptLevel = row.deptLevel+1;
                this.form.parentDeptId = row.deptId;
            },
            closeDialog(){
                this.form = {}
                this.dialogVisible = false
                this.loading = false
            },

            submit(){
                this.$refs.form.validate(async valid => {
                    if(valid){
                        try{
                            this.loading = true
                            let newDept = this.form;
                            let res = await add(this.form);
                            if(res.status === '200'){
                                this.$message.success('添加成功')
                                this.closeDialog()
                                this.$emit('success',this.parent,newDept)
                            }else{
                                this.$message.error(res.msg)
                                this.loading = false
                            }
                        }catch{
                            this.loading = false
                        }
                    }
                });
            }
        }
    }
</script>
<style lang="scss" scoped>
    .btn-group{
        display: flex;
        justify-content: right;
        .el-button{
            width: 50px;
        }
    }
    .el-form-item{
        height: 40px;
    }
</style>